package ui.panel;

import entity.CommonParam;
import ui.common.CommonPanel;
import ui.label.HeaderLabel;
import ui.label.LimpidLabel;
import ui.label.ParamLabel;
import utils.ImageUtils;

import java.io.IOException;
import java.util.*;

/**
 * @version V1.0
 * @Title:
 * @Package
 * @Description:
 * @author: runoob
 * @date:
 */
public class LinesPanel extends CommonPanel {

    private LimpidLabel bgLabel = new LimpidLabel(ImageUtils.common,0, 0, WorkPanel.width-10, WorkPanel.height-30);
    private HeaderLabel headerLabel = new HeaderLabel(5, "键", "值");
    private ParamLabel wait1Label;
    private ParamLabel wait2Label;
    private ParamLabel wait3Label;
    private ParamLabel wait4Label;
    private ParamLabel wait5Label;
    private ParamLabel love1Label;
    private ParamLabel titleLabel;
    private ParamLabel exitLabel;
    public Map<Integer, ParamLabel> paramMap = new HashMap<>();

    public LinesPanel() throws IOException {
        this.setBounds(5,81,WorkPanel.width-10,WorkPanel.height-80);
        this.add(headerLabel);
        this.add(bgLabel);
        this.setBorder(null);
    }

    public void setData(List<String> lineList) throws IOException {
        setParamLabel(lineList);
        setParamMap();
        removeComponent();
        addComponent();
        refresh();
    }

    public void setParamLabel(List<String> lineList) throws IOException {
        wait1Label = new ParamLabel(40, 0, "wait1", "", this, lineList);
        wait2Label = new ParamLabel(70, 1, "wait2", "", this, lineList);
        wait3Label = new ParamLabel(100, 2, "wait3", "", this, lineList);
        wait4Label = new ParamLabel(130, 3, "wait4", "", this, lineList);
        wait5Label = new ParamLabel(160, 4, "wait5", "", this, lineList);
        love1Label = new ParamLabel(190, 5, "love1", "", this, lineList);
        titleLabel = new ParamLabel(220, 6, "title", "", this, lineList);
        exitLabel = new ParamLabel(250, 7, "exit", "", this, lineList);
    }

    public void setParamMap(){
        paramMap.put(0, wait1Label);
        paramMap.put(1, wait2Label);
        paramMap.put(2, wait3Label);
        paramMap.put(3, wait4Label);
        paramMap.put(4, wait5Label);
        paramMap.put(5, love1Label);
        paramMap.put(6, titleLabel);
        paramMap.put(7, exitLabel);
    }

    public void removeComponent(){
        this.remove(wait1Label);
        this.remove(wait2Label);
        this.remove(wait3Label);
        this.remove(wait4Label);
        this.remove(wait5Label);
        this.remove(love1Label);
        this.remove(titleLabel);
        this.remove(exitLabel);
        this.remove(headerLabel);
        this.remove(bgLabel);
    }

    public void addComponent(){
        this.add(wait1Label);
        this.add(wait2Label);
        this.add(wait3Label);
        this.add(wait4Label);
        this.add(wait5Label);
        this.add(love1Label);
        this.add(titleLabel);
        this.add(exitLabel);
        this.add(headerLabel);
        this.add(bgLabel);
    }

    public ArrayList<CommonParam> getData(){
        ArrayList<CommonParam> result = new ArrayList<>();
        Set<Map.Entry<Integer, ParamLabel>> entries = paramMap.entrySet();
        for (Map.Entry<Integer, ParamLabel> entry : entries) {
            ParamLabel cache = entry.getValue();
            if (cache.isChosen){
                CommonParam data = new CommonParam();
                data.setId(cache.i);
                data.setKey(cache.key);
                data.setValue(cache.value);
                result.add(data);
            }
        }
        return result;
    }

    public void refresh(){
        this.invalidate();
        this.validate();
        this.repaint();
    }

}
